package com.zlsy.thread.joinlearing;

import java.util.concurrent.TimeUnit;

/**
 * @author zhouliang
 * @date 2020/3/7 18:10
 */
public class Join {
    /**
     * 展示join的基本用法。主线程会去等待子线程执行完毕再往下运行。
     *
     * @param args
     */
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "执行完毕。");
            }
        });
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "执行完毕。");
            }
        });
        thread.start();
        thread1.start();
        System.out.println("开始等待子线程运行完毕。");
        try {
            thread.join();
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("所有的子线程已经执行完毕了");
    }
}
